﻿/*
DayofDate() by Avi Aryan
	Gives the day (Sunday, Mon..) of a requested date
	
Based on Zeller's Algorithm
	http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Zeller.E2.80.99s_algorithm
	
Thanks to polyethene for DateParse()

Licensed under Apache License v2.0
-------------------------------------------------------------------------------------------------
msgbox,% DayofDate("6 June 2013")
msgbox,% DayofDate(A_Now)
msgbox,% DayofDate("2013")  ;Day of 1st Jan 2013
*/

DayofDate(Date){
if Date is Not Integer
	Date := DateParse(Date)	;parse to format

Year := SubStr(Date,1,4) 
Month := Substr(Date, 5, 2)
Day := Substr(Date,7,2)

if (Month = 1 or Month = 2)
	Year-=1 , Month+=12		;a/c Zeller's
last2ofy := Substr(Year, -1) 
first2ofy := Substr(Year, 1, 2)

dayindex := Mod( Day + Floor((Month+1)*2.6) + last2ofy + Floor(last2ofy/4) + Floor(first2ofy/4) - 2*first2ofy , 7 )

static nameofdays := "Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday"
Dayz:={}
Loop, parse, nameofdays, `,
{
	Dayz[A_index-1]:=A_LoopField
}
return Dayz[dayindex]
}


;##############################################
;	Function: DateParse by polyethene
;	Dont remove this function

DateParse(str) {
	static e2 = "i)(?:(\d{1,2}+)[\s\.\-\/,]+)?(\d{1,2}|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*)[\s\.\-\/,]+(\d{2,4})"
	str := RegExReplace(str, "((?:" . SubStr(e2, 42, 47) . ")\w*)(\s*)(\d{1,2})\b", "$3$2$1", "", 1)
	If RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?"
		. "(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?"
		. "(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", i)
		d3 := i1, d2 := i3, d1 := i4, t1 := i5, t2 := i7, t3 := i8
	Else If !RegExMatch(str, "^\W*(\d{1,2}+)(\d{2})\W*$", t)
		RegExMatch(str, "i)(\d{1,2})\s*:\s*(\d{1,2})(?:\s*(\d{1,2}))?(?:\s*([ap]m))?", t)
			, RegExMatch(str, e2, d)
	f = %A_FormatFloat%
	SetFormat, Float, 02.0
	d := (d3 ? (StrLen(d3) = 2 ? 20 : "") . d3 : A_YYYY)
		. ((d2 := d2 + 0 ? d2 : (InStr(e2, SubStr(d2, 1, 3)) - 40) // 4 + 1.0) > 0
			? d2 + 0.0 : A_MM) . ((d1 += 0.0) ? d1 : A_DD) . t1
			+ (t1 = 12 ? t4 = "am" ? -12.0 : 0.0 : t4 = "am" ? 0.0 : 12.0) . t2 + 0.0 . t3 + 0.0
	SetFormat, Float, %f%
	Return, d
}
